<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <h1>Iframe2</h1>
    <div id="message"></div>

    <script>

        // 监听index中iframe传递的消息
        window.addEventListener('message', function (event) {
            console.log('[ event ] >', event)

            let messageDom = document.getElementById('message');
            messageDom.innerHTML = "收到" + event.origin + "消息：" + event.data;


            // e.ports能拿到index传过来的port
            let port = event.ports[0];


            port.onmessage = function (e) {
                messageDom.innerHTML += '<br/>收到' + e.origin + '消息: ' + e.data
            }

            port.postMessage('from iframe2');

        })

    </script>
</body>

</html>